package com.hz.ruoyiquality.controller;

import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.ruoyiquality.domain.LgQuality;
import com.hz.ruoyiquality.domain.dto.PendingOrderDTO;
import com.hz.ruoyiquality.service.LgQualityOrderService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/goQuality")
public class LgQualityOrderController extends BaseController {

    @Autowired
    private LgQualityOrderService lgQualityOrderService;

    /**
     * 获取待检订单列表
     * @return 响应结果
     */
    @GetMapping("/list")
    public TableDataInfo getPendingOrders(@RequestParam(required = false) Integer orderNumber,
                                          @RequestParam(required = false) String goodsName,
                                          @RequestParam(required = false) String sellerName,
                                          @RequestParam(required = false) Integer status) {
        startPage();
//        lgQuality.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        List<PendingOrderDTO> orders = lgQualityOrderService.getPendingOrders(orderNumber, goodsName, sellerName, status,String.valueOf(BaseDomain.getUserId()));
        return getDataTable(orders);
    }

    /**
     * 获取对应的质检员待检订单列表
     * @return 响应结果
     */
    @GetMapping("/listByQualityMan")
    public TableDataInfo getPendingOrdersByQualityMan(@RequestParam Integer inspectorId) {
        startPage();

        // 通过服务层查询订单
        List<PendingOrderDTO> orders = lgQualityOrderService.getOrdersByInspectorId(inspectorId);
        return getDataTable(orders);
        }

    /**
     * 获取对应的质检员待   入库和出入订单列表
     * @return 响应结果
     */
    @GetMapping("/listPendingStorage")
    public TableDataInfo getPendingOrdersByQualityManStatus(@RequestParam Integer inspectorId) {
        startPage();

        // 通过服务层查询订单
        List<PendingOrderDTO> orders = lgQualityOrderService.getOrdersByInspectorIdStatus(inspectorId);
        return getDataTable(orders);
    }


    /**
     * 根据 ID 查询订单详情（回显）
     */
    @GetMapping(value = "/{orderNumber}")
    public AjaxResult getOrderById(@PathVariable Integer orderNumber) {
        return success(lgQualityOrderService.selectPendingOrderById(orderNumber));
    }

    /**
     * 订单状态更新成功，并已同步到质检记录
     */
    @Log(title = "待检订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
//    @PostMapping("/update")
    public AjaxResult edit(@Validated @RequestBody PendingOrderDTO order) {
        try {
            lgQualityOrderService.updateOrderAndInsertStatus(order);
            return success("订单状态更新成功，并已同步到质检记录！");
        } catch (Exception e) {
            return error("操作失败");
        }
    }


    /**
     * 手动触发插入待质检订单
     */
    @PostMapping("/insertPending")
    public AjaxResult insertPendingOrdersToQualityStatus() {
        try {
            lgQualityOrderService.insertPendingOrdersToQualityStatus();
            return AjaxResult.success("待质检订单已成功插入 lg_quality_status 表");
        } catch (Exception e) {
            return AjaxResult.error("插入失败：" + e.getMessage());
        }
    }

    /**
     * 绑定质检员到订单
     */
    @PostMapping("/bindQualityMan")
    public AjaxResult bindQualityManToOrder(@RequestBody PendingOrderDTO pendingOrderDTO) {
        boolean success = lgQualityOrderService.bindQualityManToOrder(pendingOrderDTO.getOrderNumber(), pendingOrderDTO.getInspectorId());
        return success ? AjaxResult.success("绑定成功") : AjaxResult.error("绑定失败");
    }
    /**
     * 表格导出数据
     */

    @Log(title = "导出数据管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("quality:goQuality:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PendingOrderDTO pendingOrderDTO)
    {
        List<PendingOrderDTO> list = lgQualityOrderService.getPendingOrders(pendingOrderDTO.getOrderNumber(),
                pendingOrderDTO.getGoodsName(),
                pendingOrderDTO.getSellerName(),
                pendingOrderDTO.getStatus(),
                pendingOrderDTO.getCreateBy());
        ExcelUtil<PendingOrderDTO> util = new ExcelUtil<PendingOrderDTO>(PendingOrderDTO.class);
        util.exportExcel(response, list, "数据");
    }

}
